//------------------------------------------------------------------------------
#include <QtGui>
#include <QMessageBox>
#include <QDate>
#include <QUuid>
#include <QFile>
#include <QTextStream>
#include <QFileDialog>
#include <QSqlRecord>
#include "CFindFrm.h"
#include "CAbonnementEMailingFrm.h"
#include "version.h"
#include "CMySQL.h"
#include "CHelp.h"
//------------------------------------------------------------------------------
CAbonnementEMailingFrm::CAbonnementEMailingFrm(QString name, QMap<QString, QVariant> lastKeys, QWidget *parent) : CMyMainWindow(name, parent) {
	setActionCur(CGlobal::acConstruct);
	
	dao=new CDAOAbonneEMailing();
	
	setupUi(this);

	setStatusBar(NULL);
	
	loadSchema();
	
	if(CDAOManager::getInstance()->getNbEnregistrement(dao) == 0) {
		setActionCur(CGlobal::acTableVide);
		keyValues["id"]=QVariant();
	}else {
		keyValues["id"]=lastKeys["id"];
		if(!chargeDonnees()) {
			chargeDonnees(CGlobal::sePremier);
		}
	}
}
//------------------------------------------------------------------------------
void CAbonnementEMailingFrm::setActionCur(CGlobal::EAction action) {
	actionCur=action;
	
	switch(actionCur) {
		case CGlobal::acNone:
			actPremiereFiche->setEnabled(true);
			actFichePrecedente->setEnabled(true);
			actFicheSuivante->setEnabled(true);
			actDerniereFiche->setEnabled(true);
			
			actAjouter->setEnabled(true);
			actSupprimer->setEnabled(true);
			
			actValider->setEnabled(false);
			actAnnuler->setEnabled(false);
			
			actRechercheUneFiche->setEnabled(true);
			
			actImport->setEnabled(true);
			
			enableChamps(true);
			break;
		case CGlobal::acTableVide:
			actPremiereFiche->setEnabled(false);
			actFichePrecedente->setEnabled(false);
			actFicheSuivante->setEnabled(false);
			actDerniereFiche->setEnabled(false);
			
			actAjouter->setEnabled(true);
			actSupprimer->setEnabled(false);
			
			actValider->setEnabled(false);
			actAnnuler->setEnabled(false);
			
			actRechercheUneFiche->setEnabled(false);
			
			actImport->setEnabled(true);
			
			clearChamps();
			enableChamps(false);
			creation->setValue("");
			break;
		case CGlobal::acAjout:
			clearChamps();
			initChamps();
			enableChamps(true);
			abonne->setCheckState(Qt::Checked);
			mail->setFocus();
		case CGlobal::acModif:
			actPremiereFiche->setEnabled(false);
			actFichePrecedente->setEnabled(false);
			actFicheSuivante->setEnabled(false);
			actDerniereFiche->setEnabled(false);
			
			actAjouter->setEnabled(false);
			actSupprimer->setEnabled(false);
			
			actValider->setEnabled(true);
			actAnnuler->setEnabled(true);
			
			actRechercheUneFiche->setEnabled(false);
			
			actImport->setEnabled(false);
		default:
			break;
	}
}
//------------------------------------------------------------------------------
CGlobal::EAction CAbonnementEMailingFrm::getActionCur(void) {
	return actionCur;
}
//------------------------------------------------------------------------------
void CAbonnementEMailingFrm::closeEvent(QCloseEvent * event) {
	if(getActionCur() != CGlobal::acNone && getActionCur() != CGlobal::acTableVide) {
		QMessageBox msgBox(	QMessageBox::Warning, tr("Attention"), 
							tr("Veuillez annuler ou valider avant de sortir"), QMessageBox::Ok);
		msgBox.exec();	
		
		event->ignore();
	}else {
		emit quit(objectName(), keyValues);
		event->accept();
	}
}
//------------------------------------------------------------------------------
void CAbonnementEMailingFrm::showFormulaire(CGlobal::EAction& action) {
	keyValues["id"]=dao->getFieldId();
	
	id->setValue(dao->getFieldId());
	mail->setValue(dao->getFieldMail());
	abonne->setValue(dao->getFieldAbonne());
	societe->setValue(dao->getFieldSociete());
	contact->setValue(dao->getFieldContact());
	creation->setValue(dao->getFieldCreation());
}
//------------------------------------------------------------------------------
void CAbonnementEMailingFrm::on_actAjouter_triggered(void) {
	setActionCur(CGlobal::acAjout);
	
	dao->toNull();
	id->setText(QUuid::createUuid().toString());
	creation->setValue(CMySQL::getInstance()->getCurrentDate());
}
//------------------------------------------------------------------------------
void CAbonnementEMailingFrm::on_actValider_triggered(void) {
	bool execOk;
	
	calculChamps(getActionCur());
	
	if(!validChamps()) return;
	if(!validLiaison()) return;
	
	dao->setFieldMail(mail->getValue());
	dao->setFieldAbonne(abonne->getValue());
	dao->setFieldSociete(societe->getValue());
	dao->setFieldContact(contact->getValue());
	dao->setFieldModification(CDAOManager::getInstance()->getCurrentDate());
	dao->setFieldModificateur(CMySQL::getInstance()->getUser());
	
	switch(getActionCur()) {
		case CGlobal::acAjout:
			dao->setFieldId(id->getValue());
			dao->setFieldCreation(CDAOManager::getInstance()->getCurrentDate());
			dao->setFieldCreateur(CMySQL::getInstance()->getUser());
			dao->setFieldNombreEnvoye(0);
			
			execOk=CDAOManager::getInstance()->insert(dao);
			break;
		case CGlobal::acModif:
			execOk=CDAOManager::getInstance()->update(dao);
			break;
		default:
			return;
	}
	
	if(execOk) {
		keyValues["id"]=dao->getFieldId();;
		setActionCur(CGlobal::acNone);
	}
}
//------------------------------------------------------------------------------
void CAbonnementEMailingFrm::on_actAnnuler_triggered(void) {
	QMessageBox msgBox(	QMessageBox::Question, tr("Confirmation"), 
						tr("Etes vous sûre de vouloir annuler ?"), QMessageBox::Yes | QMessageBox::No);
	msgBox.exec();
	
	if(msgBox.result() == QMessageBox::Yes) {
		if(!chargeDonnees(CGlobal::seNone)) {
			if(!chargeDonnees(CGlobal::sePrecedent)) {
				if(!chargeDonnees(CGlobal::seSuivant)) {
					setActionCur(CGlobal::acTableVide);
					return;
				}
			}
		}
		setActionCur(CGlobal::acNone);
	}
}
//------------------------------------------------------------------------------
void CAbonnementEMailingFrm::on_actSupprimer_triggered(void) {
	QMessageBox msgBox(	QMessageBox::Question, tr("Confirmation"), 
						tr("Etes vous sûre de vouloir supprimer la fiche courante ?"), QMessageBox::Yes | QMessageBox::No);
	msgBox.exec();
	
	if(msgBox.result() == QMessageBox::Yes) {
		CDAOManager::getInstance()->drop(dao, keyValues);
		
		if(!chargeDonnees(CGlobal::sePrecedent)) {
			if(!chargeDonnees(CGlobal::seSuivant)) {
				setActionCur(CGlobal::acTableVide);
			}
		}
	}
}
//------------------------------------------------------------------------------
void CAbonnementEMailingFrm::onChange(void) {
	if(getActionCur() == CGlobal::acNone) {
		setActionCur(CGlobal::acModif);
	}
}
//------------------------------------------------------------------------------
void CAbonnementEMailingFrm::on_actRechercheUneFiche_triggered(void) {
	CFindFrm *findFrm=new CFindFrm(masterTable, this);
	
	findFrm->setModal(true);
	findFrm->exec();
	if(findFrm->result() == QDialog::Accepted) {
		chargeDonnees(findFrm->getResult(), CGlobal::seNone);
	}
}
//------------------------------------------------------------------------------
void CAbonnementEMailingFrm::on_actImport_triggered(void) {
	QFile *file;
	QTextStream *textStream;
	QString fileName=QFileDialog::getOpenFileName(this);
	int nb=0;
	
	if(fileName != "") {
		setCursor(Qt::WaitCursor);
		file=new QFile(fileName);
		if(file->open(QIODevice::ReadOnly | QIODevice::Text)) {
			textStream=new QTextStream(file);
			while (!textStream->atEnd()) {
				QString line = textStream->readLine();
				QString requete;
				QStringList l=line.split(";"); 
				
				if(!CMySQL::getInstance()->existe(masterTable, "mail", l[2])) {	
					requete="INSERT INTO "+masterTable+" SET "+primaryKey+"=\""+QUuid::createUuid().toString()+"\", ";
					requete+="mail = \""+l[2]+"\", abonne=1, creation=CURDATE(), createur=\""+CMySQL::getInstance()->getUser()+"\", ";
					requete+="societe = \""+l[0]+"\", contact = \""+l[1]+"\" ";
					
					if(CMySQL::getInstance()->execQuery(requete) != 0) {
						nb++;
					}
				}
			}
			
			delete textStream;
			delete file;
		}
		QMessageBox msgBox(	QMessageBox::Information, tr("Information"), 
							tr("Chargement de ")+QString::number(nb)+tr(" e-mail(s) dans la base"), QMessageBox::Ok);
		msgBox.exec();	
		
		setCursor(Qt::ArrowCursor);
		
		if(nb != 0) {
			chargeDonnees("", CGlobal::sePremier);
			setActionCur(CGlobal::acNone);
		}
	}
}
//------------------------------------------------------------------------------
void CAbonnementEMailingFrm::onInvokeHelp(CChamp *champ) {
	CHelp::getInstance()->startHelp("setSource qthelp://alpes-ressources.com.coMexpert-"+VERSION_LOGICIEL+"/doc/abonneEMailing.html#"+champ->getTable()+"."+champ->getChamp());
}
//------------------------------------------------------------------------------